/* 日报统计 */
var gridObj;
var uncommittedGridObj;
var statistics = {

    /* 初始化 */
    init: function() {
        var self = this;
        self.initForm();
        self.initEvent();
        self.initGird();
        self.initChart();
    },

    /* 初始化表单 */
    initForm: function() {
        var self = this;
        // 初始化日期插件
        $('#beginDate,#endDate').datetimepicker({
            format: 'yyyy-mm-dd',
            startView: 2,
            minView: 2,
            autoclose: true,
            language: 'zh-CN',
            todayBtn: true,
            todayHighlight: true,
        });
        // 初始化下拉框
        self.getProjectList();
        self.getSubordinateList(function(){
            self.getYearList();
            self.getStatisticsGrid();
        });
        if($('#perm').val() == 4){
            self.getYearList();
        }
        // self.getYearList();
        self.getTeamUsers();
    },

    /* 初始化事件 */
    initEvent: function() {
        var self = this;

        $('#search').click(function() {
            var params = $('#statisticsForm').serializeArray();
            self.query(params, gridObj);
        });

        $('#year, #projectId').change(function() {
            self.getChart();
        });

        $('#statistics').click(function() {
            self.getChart();
        });

        $('#subordinate, #status, #project').change(function() {
            var params = $('#statisticsForm').serializeArray();
            self.query(params, gridObj);
        });

        $('#uid').change(function() {
            self.getYearList();
            self.initChart();
        });

        /* 清空日期 */
        $('.clearDate').click(function(){
            $(this).prev().val('');
        });
    },

    /* 初始化统计图 */
    initChart: function() {
        var self = this;
        setTimeout(function(){
            self.getChart();
        }, 1000);
    } ,

    /* 初始化表格 */
    initGird: function() {
        var self = this;
        if($('#perm').val() == 4){
            self.getStatisticsGrid();
        }
    },

    /**/
    getStatisticsGrid: function() {
        gridObj = $.fn.bsgrid.init('statisticsTable', {
            dataType: 'json',
            url: '/admin/statisticsQuery',
            otherParames: $('#statisticsForm').serializeArray(),
            pageSize: 10,
            pageSizeSelect: true,
            pageSizeForGrid: [5, 10, 15, 20],
            displayBlankRows: false,
            stripeRows: true,
            rowHoverColor: true
        });
    },

    /* 日报查询 */
    query: function(params, gridObj) {
        gridObj.search(params);
    },

    /* 查询当前用户负责的项目 */
    getProjectList: function() {
        $.ajax({
            url: '/staff/queryProject',
            type: 'POST',
            success: function(data) {
                $.each(data, function(index, value) {
                    $('#project, #projectId').append('<option value="' + value.id + '">' + value.name + '</option>');
                });
            }
        });
    },

    /* 查询当前用户的下属人员和自己 */
    getSubordinateList: function(callback){
        $.ajax({
            url: '/admin/querySubordinate',
            type: 'POST',
            success: function(data) {
                var perm = $('#perm').val();
                $.each(data, function(index, value) {
                    if(perm != '2' || (perm == '2' && index != 0)){
                        $('#subordinate, #uid').append('<option value="' + value.id + '">' + value.name + '</option>');
                    }
                });
                $('#subordinate').append('<option value="-1">--全部--</option>');

                if(callback) {
                    callback();
                }
            }
        });
    },

    /* 查询下属人员 */
    getTeamUsers: function(){
        var self = this;
        $.ajax({
            url: '/staff/queryTeamUser',
            type: 'POST',
            success: function(data) {
                var ids = [];
                $.each(data, function(index, value) {
                    ids.push(value.id);
                });
                $('#teamuser').val(ids + '');
                if($('#perm').val() == 2 || $('#perm').val()== 3){
                    self.getUncommittedGrid();
                }
            }
        });
    },

    /* 查询工作日未提交日报的下属 */
    getUncommittedGrid: function(){
        var params = {
            'uids' : $('#teamuser').val()
        };
        uncommittedGridObj = $.fn.bsgrid.init('uncommittedTable', {
            dataType: 'json',
            url: '/admin/uncommittedQuery',
            otherParames: params,
            pageSize: 10,
            pageSizeSelect: true,
            pageSizeForGrid: [5, 10, 15, 20],
            displayBlankRows: false,
            stripeRows: true,
        });

    },

    /* 渲染星期 */
    renderWeekday: function(record, rowIndex, colIndex, options) {
        var weekday = uncommittedGridObj.getRecordIndexValue(record, 'weekday');
        var arr = ['0', '1', '2', '3', '4', '5', '6'];
        var translateArr = ['一', '二', '三', '四', '五', '六', '日'];
        var index = arr.indexOf(weekday);
        return translateArr[index];
    },

    /* 未通过标记红色 */
    mark: function (record, rowIndex, colIndex, options) {
        var status = gridObj.getRecordIndexValue(record, 'status');
        if (status == '未通过') {
            return '<span style="color:#ff0000;">' + status + '</span>';
        } else {
            return status;
        }
    },

    /* 查询员工有日报记录的年份 */
    getYearList: function() {
        $('#year').html('');
        var params = {
            uid : $('#uid').val()
        }
        $.post('/admin/getYearList', params, function(data) {
            $.each(data, function(index, value) {
                $('#year').append('<option value="' + value.year + '">' + value.year + '</option>')
            });
        });
    },

    /* 显示折线图统计 */
    getChart: function() {
        var params = {
            id: $('#uid').val(),
            year: $('#year').val(),
            projectId: $('#projectId').val()
        };
        console.log(params);

        $.ajax({
            url : '/admin/statisticsTime',
            type : 'POST',
            data : params,
            success : function(data){
                console.log(data);
                if(data.length > 0){
                    var month = [];
                    var allTime = [];
                    var overTime = [];
                    $.each(data, function(index, value){
                        month.push(value.r_month + '月');
                        allTime.push(value.sum_all_time);
                        overTime.push(value.sum_over_time);
                    });
                }

                $('#chart').highcharts({
                    chart: {
                        type: 'line'
                    },
                    title: {
                        text: '工作量统计',
                        x: -20 //center
                    },
                    credits: {
                        enabled:false
                    },
                    subtitle: {
                        text: '年份：'+ params.year,
                        x: -20
                    },
                    tooltip: {
                        shared: true,
                        // crosshairs: true
                    },
                    plotOptions: {
                        line: {
                            dataLabels: {
                                enabled: true
                            },
                            // enableMouseTracking: false
                        }
                    },
                    xAxis: {
                        categories: month
                    },
                    yAxis: {
                        title: {
                            text: '工作量'
                        },
                        plotLines: [{
                            value: 0,
                            width: 1,
                            color: '#808080'
                        }]
                    },
                    legend: {
                        layout: 'vertical',
                        align: 'right',
                        verticalAlign: 'middle',
                        borderWidth: 0
                    },
                    series: [{
                        name: '有效工作量',
                        data: allTime
                    }, {
                        name: '加班工作量',
                        data: overTime
                    }]
                });

            },
            error : function(data){
                console.error(data);
            }
        });
    },
};

statistics.init();
